package org.mtree.context.wrapper;

import java.io.IOException;
import java.io.Reader;
import java.util.Collection;
import java.util.Properties;

import org.mtree.context.Context;

/**
 * 封装Properties对象为上下文
 * 
 * @author rushman
 * @date 2017-11-04
 */
public class PropertiesContextWrapper implements Context {

    private final Properties data;

    public PropertiesContextWrapper() {
        super();
        this.data = new Properties();
    }

    public PropertiesContextWrapper(Properties data) {
        super();
        this.data = data;
    }

    /**
     * 从字符流中加载配置信息
     * 
     * @param reader {@link Reader} 字符流
     * @throws IOException
     */
    public void load(Reader reader) throws IOException {
        this.data.load(reader);
    }

    @Override
    public Object get(String name) {
        return data.getProperty(name);
    }

    @Override
    public Object set(String name, Object value) {
        if (value != null) {
            return data.setProperty(name, value.toString());
        } else {
            Object val = data.getProperty(name);
            data.remove(name);
            return val;
        }
    }

    @Override
    public Object remove(String name) {
        return data.remove(name);
    }

    @Override
    public boolean has(String name) {
        return data.containsKey(name);
    }

    @Override
    public Collection<String> nameSet() {
        return data.stringPropertyNames();
    }

}
